//控制层
app.controller('goodsController' ,function($scope,$controller ,goodsService,uploadService,itemCatService,typeTemplateService){
	
	$controller('baseController',{$scope:$scope});//继承
	
    //读取列表数据绑定到表单中  
	$scope.findAll=function(){
		goodsService.findAll().success(
			function(response){
				$scope.list=response;
			}			
		);
	}    
	
	//分页
	$scope.findPage=function(page,rows){			
		goodsService.findPage(page,rows).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	}
	
	//查询实体 
	$scope.findOne=function(id){				
		goodsService.findOne(id).success(
			function(response){
				$scope.entity= response;					
			}
		);				
	}
	
	//保存 
	$scope.save=function(){				
		var serviceObject;//服务层对象  				
		if($scope.entity.id!=null){//如果有ID
			serviceObject=goodsService.update( $scope.entity ); //修改  
		}else{
			serviceObject=goodsService.add( $scope.entity  );//增加 
		}				
		serviceObject.success(
			function(response){
				if(response.success){
					//重新查询 
		        	$scope.reloadList();//重新加载
				}else{
					alert(response.message);
				}
			}		
		);				
	}
    //增加
    $scope.add=function(){
        goodsService.add( $scope.entity ).success(
            function(response){
                if(response.success){
                    alert(response.message);
                    $scope.entity={};
                }else{
                    alert(response.message);
                }
            }
        );
    }
	
	 
	//批量删除 
	$scope.dele=function(){			
		//获取选中的复选框			
		goodsService.dele( $scope.selectIds ).success(
			function(response){
				if(response.success){
					$scope.reloadList();//刷新列表
				}						
			}		
		);				
	}
	
	$scope.searchEntity={};//定义搜索对象 
	
	//搜索
	$scope.search=function(page,rows){			
		goodsService.search(page,rows,$scope.searchEntity).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	}

	//上传图片
	$scope.uploadFile=function () {
		uploadService.uploadFile().success(function (response) {
			if (response.success){
				//上传成功
				$scope.image_entity.url = response.message;
			}else{
				alert(response.message);
			}

        });
    }

    $scope.entity={tbGoods:{},tbGoodsDesc:{itemImages:[]}};

    //添加图片列表
	$scope.add_image_entity=function () {
		$scope.entity.tbGoodsDesc.itemImages.push($scope.image_entity);
    }

    //删除添加的图片
	$scope.remove_image_entity=function (index) {
		$scope.entity.tbGoodsDesc.itemImages.splice(index,1);
    }

    //读取一级分类
	$scope.selectItemCat1List=function () {
		itemCatService.findByParentId(0).success(function (response) {
			$scope.itemCat1List=response;
        });
    }

    //读取二级分类
    $scope.$watch('entity.tbGoods.category1Id',function (newValue,oldValue) {
	  itemCatService.findByParentId(newValue).success(function (response) {
		  $scope.itemCat2List=response;
      });
   });

    //读取三级分类
    $scope.$watch('entity.tbGoods.category2Id',function (newValue,oldValue) {
        itemCatService.findByParentId(newValue).success(function (response) {
            $scope.itemCat3List=response;
        });
    });

    //选取三级分类后,读取模板id
	$scope.$watch('entity.tbGoods.category3Id',function (newValue, oldValue) {
		itemCatService.findOne(newValue).success(function (response) {
			$scope.entity.tbGoods.typeTemplateId=response.typeId;
        })
    })

	//读取品牌
	$scope.$watch('entity.tbGoods.typeTemplateId',function (newValue, oldValue) {
		typeTemplateService.findOne(newValue).success(function (response) {
			$scope.typeTemplate=response;
            $scope.typeTemplate.brandIds = JSON.parse($scope.typeTemplate.brandIds);
            alert($scope.typeTemplate.brandIds)
        });
    })

});	

